package com.hsnn.medstgmini.sys.controller;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.std.model.HospInfo;
import com.hsnn.medstgmini.base.std.model.HospInfoDetail;
import com.hsnn.medstgmini.base.std.model.StdCompinfo;
import com.hsnn.medstgmini.base.std.model.StdCompinfoDetail;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.sys.service.SysOrgInfoManager;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 医疗机构、企业个人信息维护
 * @author 蔡春龙
 * @date 2015年7月1日
 */
@Controller
@RequestMapping("/orgInfo")
public class SysOrgInfoController {

	private static final Logger log = Logger
			.getLogger(SysOrgInfoController.class);

	@Autowired
	private HttpSession session;

	@Autowired
	private SysOrgInfoManager orgInfoManager;

	/**
	 * @category 获取当前登陆人信息
	 * @date 2015年6月14日
	 * @return
	 */
	private SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}

	/**
	 * @category 跳转医疗机构信息个人维护页面
	 * @date 2015年7月1日
	 * @return
	 */
	@RequestMapping("/toHospOrgInfo")
	public String toHospOrgInfo(Model model) {
		log.info("enter toHospOrgInfo method!");
		SysUser user = getUser();
		HospInfo hospInfo = user.getInfo();
		model.addAttribute("user", user);
		model.addAttribute("hospInfo", hospInfo);
		return "/sys/hospOrgInfo";
	}

	/**
	 * @category 保存医疗机构信息
	 * @date 2015年7月1日
	 * @param hospInfo
	 * @return
	 */
	@RequestMapping("/saveHospOrgInfo")
	@ResponseBody
	public Pagination saveHospOrgInfo(HospInfo hospInfo) {
		Pagination page = new Pagination();
		try {
			if (hospInfo != null) {
				HospInfoDetail detail = hospInfo.getHospInfoDetail();
				SysUser user = getUser();

				// 更新数据库
				hospInfo.setUpdUser(user.getId());
				orgInfoManager.saveHospOrgInfo(hospInfo);

				// 更新session
				HospInfo hi = user.getInfo();
				if (hi != null) {
					HospInfoDetail hid = hi.getHospInfoDetail();
					if (hid != null && detail != null) {
						hid.setContactName(detail.getContactName());
						hid.setContactIdnum(detail.getContactIdnum());
						hid.setContactTelephone(detail.getContactTelephone());
						hid.setContactHandphone(detail.getContactHandphone());
						hid.setContactQQ(detail.getContactQQ());
						hid.setContactEmail(detail.getContactEmail());
						hid.setContactPos(detail.getContactPos());
					}
				}

				page.setSuccess(true);
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
			page.setMsg("保存失败！");
		}
		return page;
	}

	/**
	 * @category 跳转企业信息个人维护页面
	 * @date 2015年7月1日
	 * @param model
	 * @param compType1生产企业2配送企业
	 * @return
	 */
	@RequestMapping("/toCompOrgInfo")
	public String toCompOrgInfo(Model model, int compType) {
		log.info("enter toCompOrgInfo method!");
		SysUser user = getUser();
		StdCompinfo compInfo = user.getInfo();
		model.addAttribute("user", user);
		model.addAttribute("compInfo", compInfo);
		model.addAttribute("compType", compType);
		return "/sys/compOrgInfo";
	}

	/**
	 * @category 保存企业信息
	 * @date 2015年7月1日
	 * @param compInfo
	 * @return
	 */
	@RequestMapping("/saveCompOrgInfo")
	@ResponseBody
	public Pagination saveCompOrgInfo(StdCompinfo compInfo) {
		Pagination page = new Pagination();
		try {
			if (compInfo != null) {
				StdCompinfoDetail detail = compInfo.getStdCompinfoDetail();
				SysUser user = getUser();

				// 更新数据库
				compInfo.setUpdUser(user.getId());
				orgInfoManager.saveCompOrgInfo(compInfo);

				// 更新session
				StdCompinfo ci = user.getInfo();
				if (ci != null) {
					StdCompinfoDetail cid = ci.getStdCompinfoDetail();
					if (cid != null && detail != null) {
						cid.setContactName(detail.getContactName());
						cid.setContactIdnum(detail.getContactIdnum());
						cid.setContactTelephone(detail.getContactTelephone());
						cid.setContactHandphone(detail.getContactHandphone());
						cid.setContactQq(detail.getContactQq());
						cid.setContactEmail(detail.getContactEmail());
						cid.setContactPos(detail.getContactPos());
					}
				}
				page.setSuccess(true);
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
			page.setMsg("保存失败！");
		}
		return page;
	}

}
